package com.hand.wfive.util;

import android.text.TextUtils;
import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class DateUtils {
    /**
     * 获得当前日期 yyyy-MM-dd HH:mm:ss
     *
     * @return 2019-08-27 14:12:40
     */
    public static String getCurrentTime() {
        // 小写的hh取得12小时，大写的HH取的是24小时
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        return df.format(date);
    }

    /**
     * 获取系统当前时间戳
     *
     * @return 1566889186583
     */
    public static String getSystemTime() {
        String current = String.valueOf(System.currentTimeMillis());
        return current;
    }


    /**
     * 获取当前日期 yy-MM-dd
     *
     * @return 2019-08-27
     */
    public static String getDateByString() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        return sdf.format(date);
    }

    /**
     * 获取当前日期 yy-MM-dd
     *
     * @return 2019-08-27
     */
    public static String getDateByString(String format) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    //获取昨天日期
    public static String getyesterday() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1); //向前走一天
        Date date = calendar.getTime();
        return sdf.format(date);
    }


    /**
     * 获取当前日期 yy-MM-dd
     *
     * @return 2019-08-27
     */
    public static String getDateByStringyy() {

        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        return sdf.format(date);
    }


    public static String getDateByStringyy(String date) {

        SimpleDateFormat tdf = new SimpleDateFormat("yyyy");


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");

        Date _date = null;

        try {
            _date = sdf.parse(date);

        } catch (ParseException e) {
            e.printStackTrace();

        }

        Calendar cal = Calendar.getInstance();
        cal.setTime(_date);
        return tdf.format(_date);
    }


    /**
     * 获取当前日期 yy-MM-dd
     *
     * @return 2019-08-27
     */
    public static String getDateByStringmm() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("MM");
        return sdf.format(date);
    }

    public static String getDateByStringdd() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("dd");
        return sdf.format(date);
    }

    public static String getDateByStringst(Date dates, String st) {

        SimpleDateFormat sdf = new SimpleDateFormat(st);
        return sdf.format(dates);
    }

    public static String getDateByStringmm(String date) {

        SimpleDateFormat tdf = new SimpleDateFormat("MM");


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");

        Date _date = null;

        try {
            _date = sdf.parse(date);

        } catch (ParseException e) {
            e.printStackTrace();

        }

        Calendar cal = Calendar.getInstance();
        cal.setTime(_date);
        return tdf.format(_date);
    }

    public static String getDateByStringdd(String date) {

        SimpleDateFormat tdf = new SimpleDateFormat("dd");


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");

        Date _date = null;

        try {
            _date = sdf.parse(date);

        } catch (ParseException e) {
            e.printStackTrace();

        }

        Calendar cal = Calendar.getInstance();
        cal.setTime(_date);
        return tdf.format(_date);
    }

    /**
     * 得到两个时间差  格式yyyy-MM-dd HH:mm:ss
     *
     * @param start 2019-06-27 14:12:40
     * @param end   2019-08-27 14:12:40
     * @return 5270400000
     */
    public static long dateSubtraction(String start, String end) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date1 = df.parse(start);
            Date date2 = df.parse(end);
            return date2.getTime() - date1.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 得到两个时间差  格式yyyy-MM-dd HH:mm:ss
     *
     * @param start 2019-06-27 14:12:40
     * @param end   2019-08-27 14:12:40
     * @return 5270400000
     */
    public static String dateSubtractions(String start, String end) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date1 = df.parse(start);
            Date date2 = df.parse(end);

            long between = (date2.getTime() - date1.getTime()) / 1000;//除以1000是为了转换成秒
            long day1 = between / (24 * 3600);
            long hour1 = between % (24 * 3600) / 3600;
            long minute1 = between % 3600 / 60;
            long second1 = between % 60;
            ;

            String st = "";
            if (day1 != 0) {
                st = day1 + "天";
            }
            if (hour1 != 0) {
                st += hour1 + "小时";
            }
            if (minute1 != 0) {
                st += minute1 + "分钟";
            }
            if (TextUtils.isEmpty(st)) {
                st += second1 + "秒";
            }
            return st;
        } catch (ParseException e) {
            e.printStackTrace();
            return "暂无数据";
        }
    }

    public static Map<String, Integer> calculateTimeDifference(String start) {

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date1 = df.parse(start);
            Date date2 = new Date();
            Map<String, Integer> map = new HashMap<String, Integer>();
            Calendar from = Calendar.getInstance();
            from.setTime(date1);
            Calendar to = Calendar.getInstance();
            to.setTime(date2);
            int fromYear = from.get(Calendar.YEAR);
            int fromMonth = from.get(Calendar.MONTH);
            int toYear = to.get(Calendar.YEAR);
            int toMonth = to.get(Calendar.MONTH);
            int year = 0;
            int month = toYear * 12 + toMonth - (fromYear * 12 + fromMonth);
            if (month >= 12) {
                year = toYear - fromYear;
            }
            int day = (int) ((to.getTimeInMillis() - from.getTimeInMillis()) / (24 * 60 * 60 * 1000));
            int week = (int) ((to.getTimeInMillis() - from.getTimeInMillis()) / (24 * 60 * 60 * 1000 * 7));
            int hour = (int) ((to.getTimeInMillis() - from.getTimeInMillis()) / (1000 * 60 * 60));
            int minute = (int) ((to.getTimeInMillis() - from.getTimeInMillis()) / (1000 * 60));
            int second = (int) ((to.getTimeInMillis() - from.getTimeInMillis()) / (1000));
            map.put("year", year);
            map.put("month", month);
            map.put("day", day);
            map.put("week", week);
            map.put("hour", hour);
            map.put("minute", minute);
            map.put("second", second);

            return map;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }


    }

    /**
     * 得到两个时间差  格式yyyy-MM-dd
     *
     * @param start 2019-06-27
     * @param end   2019-08-27
     * @return 天
     */
    public static long getcoutday(String start, String end) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd s");
        try {
            Date date1 = df.parse(start);
            Date date2 = df.parse(end);

            long between = (date2.getTime() - date1.getTime()) / 1000;//除以1000是为了转换成秒
            long day1 = between / (24 * 3600);
            long hour1 = between % (24 * 3600) / 3600;
            long minute1 = between % 3600 / 60;
            long second1 = between % 60 / 60;
            long st;
            if (day1 != 0) {
                st = day1;
            } else {
                st = (long) 1;
            }

            return st;
        } catch (ParseException e) {
            e.printStackTrace();
            return (long) 1;
        }
    }

    /**
     * 判断两个时间是否相隔一天
     */
    public static boolean overOneDay(long startTime, long endTime) {
        long between = endTime - startTime;
        if (between > (24 * 3600000)) {
            return true;
        }
        return false;
    }

    /**
     * 获取查询月份的天数
     *
     * @param date
     * @return
     */

    public static int getDaysOfMonth(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");

        Date _date = null;

        try {
            _date = sdf.parse(date);

        } catch (ParseException e) {
            e.printStackTrace();

        }

        Calendar calendar = Calendar.getInstance();

        calendar.setTime(_date);

        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

    }


    /**
     * 获取上一个月
     *
     * @return
     */

    public static String getLastMonth(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");

        Date _date = null;

        try {
            _date = sdf.parse(date);

        } catch (ParseException e) {
            e.printStackTrace();

        }

        Calendar cal = Calendar.getInstance();

        cal.setTime(_date);

        cal.add(Calendar.DATE, -7);
        String preMonth = sdf.format(cal.getTime());

        return preMonth;

    }

    public static String getLasttoweek(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        SimpleDateFormat tdf = new SimpleDateFormat("MM月dd日");

        Date _date = null;

        try {
            _date = sdf.parse(date);

        } catch (ParseException e) {
            e.printStackTrace();

        }

        Calendar cal = Calendar.getInstance();

        cal.setTime(_date);
        String startMonth = tdf.format(cal.getTime());

        cal.add(Calendar.DATE, -7);
        String preMonth = tdf.format(cal.getTime());

        return startMonth + "至" + preMonth;

    }

    public static String getformatdate(String date, String simpledateformat) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat tdf = new SimpleDateFormat(simpledateformat);

        Date _date = null;

        try {
            _date = sdf.parse(date);

        } catch (ParseException e) {
            e.printStackTrace();

        }
        Calendar cal = Calendar.getInstance();

        cal.setTime(_date);
        String preMonth = tdf.format(cal.getTime());

        return preMonth;

    }

    public static String getformatdate(String date, String informat, String outformat) {
        SimpleDateFormat sdf = new SimpleDateFormat(informat);
        SimpleDateFormat tdf = new SimpleDateFormat(outformat);

        Date _date = null;

        try {
            _date = sdf.parse(date);

        } catch (ParseException e) {
            e.printStackTrace();

        }
        Calendar cal = Calendar.getInstance();

        cal.setTime(_date);
        String preMonth = tdf.format(cal.getTime());

        return preMonth;

    }

    public static ArrayList<String> getLastweekday(String date) {
        ArrayList<String> list = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        SimpleDateFormat tdf = new SimpleDateFormat("yyyy-MM-dd");

        Date _date = null;

        try {
            _date = sdf.parse(date);

        } catch (ParseException e) {
            e.printStackTrace();

        }

        Calendar cal = Calendar.getInstance();
        cal.setTime(_date);
        cal.add(Calendar.DATE, -6);

        String preMonth = tdf.format(cal.getTime());
        cal.add(Calendar.DATE, 1);
        String preMonth1 = tdf.format(cal.getTime());
        cal.add(Calendar.DATE, 1);
        String preMonth2 = tdf.format(cal.getTime());
        cal.add(Calendar.DATE, 1);
        String preMonth3 = tdf.format(cal.getTime());
        cal.add(Calendar.DATE, 1);
        String preMonth4 = tdf.format(cal.getTime());
        cal.add(Calendar.DATE, 1);
        String preMonth5 = tdf.format(cal.getTime());
        cal.add(Calendar.DATE, 1);
        String preMonth6 = tdf.format(cal.getTime());
        list.add(preMonth);
        list.add(preMonth1);
        list.add(preMonth2);
        list.add(preMonth3);
        list.add(preMonth4);
        list.add(preMonth5);
        list.add(preMonth6);
        return list;

    }

    /**
     * 描述:获取下一个月.
     *
     * @return
     */
    public static String getPreMonth(String date) {
        if (isThisTime(date)) {
            return date;
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");

        Date _date = null;

        try {
            _date = sdf.parse(date);

        } catch (ParseException e) {
            e.printStackTrace();

        }

        Calendar cal = Calendar.getInstance();
        cal.setTime(_date);
        cal.add(cal.DATE, 7);
        String preMonth = sdf.format(cal.getTime());
        return preMonth;
    }

    //判断选择的日期是否是本月

    public static boolean isThisTime(String dates) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");

        Date _date = null;

        try {
            _date = sdf.parse(dates);

        } catch (ParseException e) {
        }
        String param = sdf.format(_date);//参数时间
        String now = sdf.format(new Date());//当前时间
        if (param.equals(now)) {
            return true;
        }
        return false;
    }


    /**
     * 得到两个时间差
     *
     * @param start 开始时间
     * @param end   结束时间
     * @return
     */
    public static long dateTogether(Date start, Date end) {


        return end.getTime() - start.getTime();
    }

    /**
     * 转化long值的日期为yyyy-MM-dd  HH:mm:ss.SSS格式的日期
     *
     * @param millSec 日期long值  5270400000
     * @return 日期，以yyyy-MM-dd  HH:mm:ss.SSS格式输出 1970-03-03  08:00:00.000
     */
    public static String transferLongToDate(String millSec) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss.SSS");
        Date date = new Date(Long.parseLong(millSec));
        return sdf.format(date);
    }

    /**
     * 转化long值的日期为HH:mm:ss格式的时间
     *
     * @param millSec 日期long值  5270400000
     * @return 日期，以yyyy-MM-dd  HH:mm:ss.SSS格式输出 1970-03-03  08:00:00.000
     */
    public static String transferLongToHour(long millSec) {
        long second = millSec / 1000;
        long nHour = second / 3600;
        long nMin = second % 3600;
        long nSec = nMin % 60;
        nMin = nMin / 60;
        nMin += 1;
        Log.d("transferLongToHour", "transferLongToHour: " + millSec + "    ");
        return "" + nHour + "时" + nMin + "分";
    }

    /**
     * 转化long值的日期为HH:mm:ss格式的时间
     *
     * @param millSec 日期long值  5270400000
     * @return 时分，以 mm:ss 格式输出 00:00
     */
    public static String transferLongToMin(long millSec) {
        long second = millSec / 1000;
        long nMin = second / 60;
        long nSec = second % 60;
        String minStr = "00";
        if (nMin < 10) {
            minStr = "0" + nMin;
        } else {
            minStr = "" + nMin;
        }
        String secStr = "00";
        if (nSec < 10) {
            secStr = "0" + nSec;
        } else {
            secStr = "" + nSec;
        }
        Log.d("transferLongToHour", "transferLongToHour: " + millSec + "    ");
        return minStr + ":" + secStr;
    }

    /**
     * 获得当前日期 yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public static String getOkDate(String date) {
        try {
            if (TextUtils.isEmpty(date)) {
                return null;
            }
            Date date1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH).parse(date);
            //格式化
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sdf.format(date1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 获取当前日期是一个星期的第几天
     *
     * @return 2
     */
    public static int getDayOfWeek() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        return cal.get(Calendar.DAY_OF_WEEK) - 1;
    }


    /**
     * 判断当前时间是否在[startTime, endTime]区间，注意时间格式要一致
     *
     * @param nowTime     当前时间
     * @param dateSection 时间区间   2018-01-08,2019-09-09
     * @return
     * @author jqlin
     */
    public static boolean isEffectiveDate(Date nowTime, String dateSection) {
        try {
            String[] times = dateSection.split(",");
            String format = "yyyy-MM-dd";
            Date startTime = new SimpleDateFormat(format).parse(times[0]);
            Date endTime = new SimpleDateFormat(format).parse(times[1]);
            if (nowTime.getTime() == startTime.getTime()
                    || nowTime.getTime() == endTime.getTime()) {
                return true;
            }
            Calendar date = Calendar.getInstance();
            date.setTime(nowTime);

            Calendar begin = Calendar.getInstance();
            begin.setTime(startTime);

            Calendar end = Calendar.getInstance();
            end.setTime(endTime);

            if (isSameDay(date, begin) || isSameDay(date, end)) {
                return true;
            }
            if (date.after(begin) && date.before(end)) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean isSameDay(Calendar cal1, Calendar cal2) {
        if (cal1 != null && cal2 != null) {
            return cal1.get(0) == cal2.get(0) && cal1.get(1) == cal2.get(1) && cal1.get(6) == cal2.get(6);
        } else {
            throw new IllegalArgumentException("The date must not be null");
        }
    }

    public static long getTimeByDate(String time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = format.parse(time);
            //日期转时间戳（毫秒）
            return date.getTime();
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    public static void getTimeByDates(String time) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = format.parse(time);
            Calendar c = Calendar.getInstance();
            c.setTime(new Date());
//将date - 7
            c.add(Calendar.DATE, -7);
            Date d = c.getTime();
            String day = format.format(d);


            //日期转时间戳（毫秒）
//            return date.getTime();
        } catch (Exception e) {
            e.printStackTrace();
//            return 0;
        }
    }

    /**
     * 判断是否为今天(效率比较高)
     *
     * @param day 传入的 时间  "2016/06/28 10:10:30" "2016/06/28" 都可以
     * @return true今天 false不是
     * @throws ParseException
     */
    public static boolean isToday(String day) throws ParseException {
        if (TextUtils.isEmpty(day)) {
            return false;
        }
        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);

        Calendar cal = Calendar.getInstance();
        Date date = getDateFormat().parse(day);
        cal.setTime(date);

        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                    - pre.get(Calendar.DAY_OF_YEAR);

            if (diffDay == 0) {
                return true;
            }
        }
        return false;
    }

    private static ThreadLocal<SimpleDateFormat> DateLocal = new ThreadLocal<SimpleDateFormat>();

    public static SimpleDateFormat getDateFormat() {
        if (null == DateLocal.get()) {
            DateLocal.set(new SimpleDateFormat("yyyy/MM/dd", Locale.CHINA));
        }
        return DateLocal.get();
    }

    /**
     * 获取几天后的时间
     *
     * @param day the day
     * @return the date after
     */
    public static String getAfterDay(int day) {
        Calendar calendar2 = Calendar.getInstance();
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
        calendar2.add(Calendar.DATE, day);
        return sdf2.format(calendar2.getTime());
    }
}
